home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / other / msgstrip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-02-01  |  2.1 KB  |  98 lines

  1. /* msgstrip:  remove lines of control-A and all message header text       */
  2.  
  3. #include "util.h"
  4. #include "mmdf.h"
  5.  
  6. extern char *delim1;
  7.  
  8. FILE *tmpout,
  9.      *filin;
  10. int msgnum;
  11. int filnum;
  12.  
  13. char    tmpfil[] = ",msgstrip";
  14.  
  15. char tmpstr[64];
  16. char    buf[512];
  17. char doingheader;
  18. char hadtext;
  19.  
  20.  
  21. main (argc, argv)
  22. int     argc;
  23. char   *argv[];
  24. {
  25.     char  *thefile;
  26.  
  27.     mmdf_init (argv[0]);
  28.     for (argc--; filnum++ < argc; )
  29.     {                             /* process a message file/folder        */
  30.     printf ("%s: ", thefile = argv[filnum]);
  31.     if ((tmpout = fopen (tmpfil, "w")) == NULL)
  32.         endit ("Unable to creat temp");
  33.  
  34.     if ((filin = fopen (thefile, "r")) == NULL)
  35.     {
  36.         printf ("Unable to open file\n");
  37.         continue;
  38.     }
  39.  
  40.     for (msgnum = 1; !feof (filin); )
  41.     {                         /* process a message                    */
  42.         for (doingheader = TRUE, hadtext = FALSE;  ; )
  43.         switch (fgets (buf, sizeof buf, filin))
  44.         {                 /* process a line                       */
  45.             case NULL:
  46.             if (ferror (filin))
  47.                 printf ("Error reading during header skip\n");
  48.             goto nxtmsg;
  49.  
  50.             default:
  51.             if (doingheader && buf[0] == '\n')
  52.             {   doingheader = FALSE;
  53.                 continue;
  54.             }         /* drop on through                      */
  55.             if (strcmp (buf, delim1) == 0)
  56.                 goto nxtmsg;
  57.             if (doingheader)
  58.                 continue;
  59.             hadtext = TRUE;
  60.             fputs (buf, tmpout);
  61.         }
  62. nxtmsg:
  63.         if (hadtext)
  64.         printf ("%d, ", msgnum++);
  65.     }
  66.  
  67.     fflush (tmpout);
  68.     if (ferror (tmpout))
  69.         endit ("Error writing");
  70.     sprintf (tmpstr, ",%s", thefile);
  71.  
  72.     if (link (thefile, tmpstr) < 0 &&
  73.         unlink (tmpstr) &&
  74.         link (thefile, tmpstr) < 0)
  75.         endit ("Unable to link to backup");
  76.     if (unlink (thefile) < 0)
  77.         endit ("Unable to unlink file");
  78.     if (link (tmpfil, thefile) < 0)
  79.         endit ("Unable to link temporary into old file name");
  80.     if (unlink (tmpfil) < 0)
  81.         endit ("Unable to unlink temporary file");
  82.     sprintf (tmpstr, ".{%s", thefile);
  83.     unlink (tmpstr);          /* get rid of parsefile                 */
  84.     printf ("ok\n");
  85.  
  86.     fclose (tmpout);
  87.     fclose (filin);
  88.     }
  89. }
  90.  
  91. endit (str)
  92. char    str[];
  93. {
  94.     printf ("%s\n", str);
  95.     fflush (stdout);
  96.     exit (-1);
  97. }
  98.